Conditions | 16 |
Paths | 14 |
Total Lines | 58 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like calculator.js ➔ key_detect_calc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | calc_array = new Array(); |
||
111 | function key_detect_calc(id,evt) |
||
112 | { |
||
113 | if((evt.keyCode>95) && (evt.keyCode<106)) |
||
114 | { |
||
115 | var nbr = evt.keyCode-96; |
||
116 | add_calc(id,nbr); |
||
117 | } |
||
118 | else if((evt.keyCode>47) && (evt.keyCode<58)) |
||
119 | { |
||
120 | var nbr = evt.keyCode-48; |
||
121 | add_calc(id,nbr); |
||
122 | } |
||
123 | else if(evt.keyCode==107) |
||
124 | { |
||
125 | f_calc(id,'+'); |
||
126 | } |
||
127 | else if(evt.keyCode==109) |
||
128 | { |
||
129 | f_calc(id,'-'); |
||
130 | } |
||
131 | else if(evt.keyCode==106) |
||
132 | { |
||
133 | f_calc(id,'*'); |
||
134 | } |
||
135 | else if(evt.keyCode==111) |
||
136 | { |
||
137 | f_calc(id,''); |
||
138 | } |
||
139 | else if(evt.keyCode==110) |
||
140 | { |
||
141 | add_calc(id,'.'); |
||
142 | } |
||
143 | else if(evt.keyCode==190) |
||
144 | { |
||
145 | add_calc(id,'.'); |
||
146 | } |
||
147 | else if(evt.keyCode==188) |
||
148 | { |
||
149 | add_calc(id,'.'); |
||
150 | } |
||
151 | else if(evt.keyCode==13) |
||
152 | { |
||
153 | f_calc(id,'='); |
||
154 | } |
||
155 | else if(evt.keyCode==46) |
||
156 | { |
||
157 | f_calc(id,'ce'); |
||
158 | } |
||
159 | else if(evt.keyCode==8) |
||
160 | { |
||
161 | f_calc(id,'nbs'); |
||
162 | } |
||
163 | else if(evt.keyCode==27) |
||
164 | { |
||
165 | f_calc(id,'ce'); |
||
166 | } |
||
167 | return true; |
||
168 | } |